package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.service.MarketCouponService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

public class MarketCouponServiceImpl implements MarketCouponService {
    @Override
    public List<MarketCouponUser> listUser(String page, String limit, String couponId, String userId, String status, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponUserMapper mapper = session.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample userExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = userExample.or();
        criteria.andCouponIdEqualTo(Integer.parseInt(couponId));
//        userId和status为非必须参数有可能传null
        if (userId != null && status != null) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId))
                    .andStatusEqualTo(Short.parseShort(status));
        }
        userExample.orderBy(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCouponUser> marketCouponUsers = mapper.selectByExample(userExample);
        session.commit();
        session.close();
        return marketCouponUsers;

    }

    @Override
    public List<MarketCoupon> list(String page, String limit, String name, String type, String status, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(name!=null&&!"".equals(name))
            criteria.andNameLike("%"+name+"%");
        if(type!=null&&!"".equals(type))
            criteria.andTypeEqualTo(Short.parseShort(type));
        if(status!=null&&!"".equals(status))
            criteria.andStatusEqualTo(Short.parseShort(status));
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketCoupon> marketCoupons = mapper.selectByExample(example);
        session.commit();
        session.close();
        return marketCoupons;
    }

    @Override
    public MarketCoupon readCoupon(String id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon readCoupon = mapper.selectByPrimaryKey(Integer.parseInt(id));
        session.commit();
        session.close();
        return readCoupon;
    }

    @Override
    public MarketCoupon updateCoupon(MarketCoupon marketCoupon) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        mapper.updateByPrimaryKey(marketCoupon);
        session.commit();
        session.close();
        return marketCoupon;
    }

    @Override
    public void deleteCoupon(MarketCoupon marketCoupon) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
//        将要被修改的优惠券的逻辑删除条件设为true
        marketCoupon.setDeleted(true);
//        逻辑删除
        mapper.updateByPrimaryKeySelective(marketCoupon);
        session.commit();
        session.close();
    }

    @Override
    public void create(MarketCoupon marketCoupon) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setDeleted(false);
        marketCoupon.setUpdateTime(LocalDateTime.now());
        mapper.insertSelective(marketCoupon);
        session.commit();
        session.close();
    }
}
